//
//  TitleView.m
//  FloatClouds
//
//  Created by 黄威 on 16/8/30.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "TitleView.h"
#import "BaseButton.h"
#import "Masonry.h"
@interface TitleView ()



@end

@implementation TitleView

- (instancetype)init{
    
    self = [super init];
    
    if (self) {
        
        self.buttonOfHot = [BaseButton buttonWithType:UIButtonTypeCustom];
        [self.buttonOfHot setTitle:@"热点" forState:UIControlStateNormal];
        [self.buttonOfHot setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addSubview:self.buttonOfHot];
        
        [self.buttonOfHot mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.mas_width).multipliedBy(0.2);
            make.height.mas_equalTo (40);
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(0);
        }];
        [self.buttonOfHot addTarget:self action:@selector(Hot) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        
        self.buttonOfTest = [BaseButton buttonWithType:UIButtonTypeCustom];
        [self.buttonOfTest setTitle:@"测评" forState:UIControlStateNormal];
        [self.buttonOfTest setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addSubview:self.buttonOfTest];
        [self.buttonOfTest mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.mas_width).multipliedBy(0.2);
            make.height.mas_equalTo (40);
            make.left.mas_equalTo(self.buttonOfHot.mas_right).offset(0);
            make.top.mas_equalTo(0);
        }];
        [self.buttonOfTest addTarget:self action:@selector(Test) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        
        self.buttonOfRank = [BaseButton buttonWithType:UIButtonTypeCustom];
        [self.buttonOfRank setTitle:@"排行" forState:UIControlStateNormal];
        [self.buttonOfRank setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addSubview:self.buttonOfRank];
        [self.buttonOfRank mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.mas_width).multipliedBy(0.2);
            make.height.mas_equalTo (40);
            make.left.mas_equalTo(self.buttonOfTest.mas_right).offset(0);
            make.top.mas_equalTo(0);
        }];
        [self.buttonOfRank addTarget:self action:@selector(Rank) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        self.buttonOfViewPoint = [BaseButton buttonWithType:UIButtonTypeCustom];
        [self.buttonOfViewPoint  setTitle:@"视点" forState:UIControlStateNormal];
        [self.buttonOfViewPoint  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [self addSubview:self.buttonOfViewPoint ];
        [self.buttonOfViewPoint mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.mas_width).multipliedBy(0.2);
            make.height.mas_equalTo (40);
            make.left.mas_equalTo(self.buttonOfRank.mas_right).offset(0);
            make.top.mas_equalTo(0);
        }];
        [self.buttonOfViewPoint addTarget:self action:@selector(Point) forControlEvents:UIControlEventTouchUpInside];
        
        
        
        
        
        self.viewOfBottom = [[UIView alloc]init];
        self.viewOfBottom.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.viewOfBottom];
        [self.viewOfBottom mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(self.mas_width).multipliedBy(0.2);
            make.height.mas_equalTo (3);
            make.centerX.mas_equalTo(self.buttonOfHot);
            make.bottom.mas_equalTo(-5);
        }];
        
        
        
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(execute:)
                                                 name:@"NOTIFICATION_FanPage"
                                               object:nil];
    
    
    
    
    return self;
    
}

#pragma mark - 根据前页scroll偏移量 移动本页滑动条

- (void)execute:(NSNotification *)notification

{
    
    UIScrollView *scrollView = [notification object];
    
    int a =  scrollView.contentOffset.x / self.bounds.size.width;
    
    
    
    switch (a) {
        case 0:
            [self Hot];
            break;
        case 1:
            [self Test];
            break;
        case 2:
            [self Rank];
            break;
        case 3:
            [self Point];
            break;
            
        default:
            break;
    }
    
    
}

- (void)Hot{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:self.buttonOfHot];
    
    [self.viewOfBottom mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.mas_width).multipliedBy(0.2);
        make.height.mas_equalTo (3);
        make.centerX.mas_equalTo(self.buttonOfHot);
        make.bottom.mas_equalTo(-5);
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
        
    }];
    
}
- (void)Test{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:self.buttonOfTest];
    
    [self.viewOfBottom mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.mas_width).multipliedBy(0.2);
        make.height.mas_equalTo (3);
        make.centerX.mas_equalTo(self.buttonOfTest);
        make.bottom.mas_equalTo(-5);
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
        
    }];
    
    
}
- (void)Rank{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:self.buttonOfRank];
    [self.viewOfBottom mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.mas_width).multipliedBy(0.2);
        make.height.mas_equalTo (3);
        make.centerX.mas_equalTo(self.buttonOfRank);
        make.bottom.mas_equalTo(-5);
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
        
    }];
    
}

- (void)Point{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"test" object:self.buttonOfViewPoint];
    [self.viewOfBottom mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.mas_width).multipliedBy(0.2);
        make.height.mas_equalTo (3);
        make.centerX.mas_equalTo(self.buttonOfViewPoint);
        make.bottom.mas_equalTo(-5);
        [self setNeedsUpdateConstraints];
        [self updateConstraintsIfNeeded];
        
    }];
    
    
}



-(void)dealloc{

    [[NSNotificationCenter defaultCenter ]removeObserver:self  name:@"NOTIFICATION_FanPage" object:nil];


}





@end
